/*
 * Copyright (c) 2018, apexes.net. All rights reserved.
 *
 *         http://www.apexes.net
 *
 */
package net.apexes.maven;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;

/**
 * @author hedyn
 */
public class MavensDemo {

    private static final Logger LOG = LoggerFactory.getLogger(MavensDemo.class);

    public static void main(String... args) throws Exception {
        List<Dependency> dependencies = Arrays.asList(
                new Dependency("org.ow2.asm", "asm-all", "5.2"),
                new Dependency("io.github.lukehutch", "fast-classpath-scanner", "2.0.18"),
                new Dependency("net.apexes.commons", "apexes-commons-lang", "0.0.2")
        );
        List<URI> repositories = Arrays.asList(
                new URI("http://maven.aliyun.com/nexus/content/groups/public")
        );
        MavenDownloader downloader = Mavens.withDownloadPath(Paths.get(".", ".temp", "libs"))
                .withRepositories(repositories)
                .withDependencies(dependencies)
                .withDownloadCallback(newDownloadCallback())
                .build();
        List<Path> downloaded = downloader.downloadAll();
        LOG.info("{}", downloaded);
    }

    private static DownloadCallback newDownloadCallback() {
        return new DownloadCallback() {

            @Override
            public void onSuccess(URI repositoryUri, Dependency dependency, Path dependencyPath) {
                LOG.debug("[{}] from {} to {} success!", dependency, repositoryUri, dependencyPath);
            }

            @Override
            public void onFailure(URI repositoryUri, Dependency dependency, IOException exception) {
                LOG.info("[" + dependency + "] from " + repositoryUri + " failed!", exception.getMessage());
            }

            @Override
            public void onSuccess(Dependency dependency, Path dependencyPath) {
                LOG.info("Download [{}] to {} success!", dependency, dependencyPath);
            }

            @Override
            public void onFailure(Dependency dependency) {
                LOG.error("Download [{}] failed!", dependency);
            }
        };
    }
}
